package com.codemeta.bokit.comm.global;

import lombok.Data;

/**
 * 响应结果和错误码
 *
 * @author hice
 */
@Data
public class ResponseResult<T> {

    /**
     * 响应数据
     */
    private T data;
    /**
     * 响应码
     */
    private String code;
    /**
     * 响应描述
     */
    private String message;

    public ResponseResult(ErrorCode status) {
        this.code = status.getCode();
        this.message = status.getMessage();
    }

    public ResponseResult(ErrorCode status, T data) {
        this.data = data;
        this.code = status.getCode();
        this.message = status.getMessage();
    }

    public ResponseResult(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseResult(T data, String code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

    public static <T> ResponseResult<T> ok() {
        return new ResponseResult<>(DefaultErrorCode.ok.getCode(), "success");
    }

    public static <T> ResponseResult<T> ok(T data) {
        return new ResponseResult<>(data, DefaultErrorCode.ok.getCode(), "success");
    }

    public static <T> ResponseResult<T> fail(ErrorCode status, T data) {
        return new ResponseResult<>(status, data);
    }

    public static <T> ResponseResult<T> fail(String code, String message) {
        return new ResponseResult<>(null, code, message);
    }

    @Override
    public String toString() {
        return "errCode=" + code + "errMsg=" + message + "data=" + data;
    }
}